import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Data base entity describe alphabet in language.
 *  
 * @author anton
 *
 */
public class EW_Alphabet extends DataBaseAccess {
	private String letter = ""; // current letter
	private String languageName = ""; // language name
	private int wordsCount = 0; // words for current letter
	
	/**
	 * return current letter
	 * @return
	 */
	public String getLetter(){
		return letter;
	}
	
	/**
	 * get words count for current letter
	 * @return - words count
	 */
	public int getWordsCount(){
		return wordsCount;
	}
	
	/**
	 * set counts of words for current letter
	 * @param newCount - new count of word
	 */
	public void setWordsCount(int newCount){
		wordsCount = newCount;
	}
	
	/**
	 * return current language for current letter
	 * @return - language name
	 */
	public String getLanguage() {
		return languageName;
	}
	
	/**
	 * constructor for one letter
	 * @param language - language for latter
	 * @param oneLetter - current letter
	 */
	public EW_Alphabet (String language, String oneLetter) {
		super("Alphabet");
		HashMap<String, String> key = new HashMap<String, String>();
		languageName = language;
		letter = oneLetter.toLowerCase();
		// try to get letter from db if it's not in then add.
		if (!letter.trim().equals("")){
			key.put("letter", letter);
			key.put("language", languageName);
			if (getOneItemByKey(key) == null){
				insertCurRecord();
			}
		}
	}
	
	@Override
	protected final ResultSet getListByDetail(HashMap<?, ?> keys) {
		String selectSQL = "SELECT * From Alphabet ";
		if (keys.get("letter") != null) {
			selectSQL += "where letter = '" +	keys.get("letter") + "'";
			if (keys.get("language") != null) selectSQL += " and language = '" + keys.get("language") + "'";
		} else {
			if (keys.get("language") != null) selectSQL += "where language = '" + keys.get("language") + "'";
		}
		
		try {
			EWDataBase db = EWDataBase.getInstance();
			return db.conn.prepareStatement(selectSQL).executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * return list of all item's which satisfy the request of keys
	 * @param keys	array of request parameters key can be:
	 * 	1 - language get string, name of language
	 *  2 - letter get string, letter sign 
	 * @return	list of EW_Alphabet.
	 */
	public ArrayList<EW_Alphabet> getList(HashMap<String, String> keys){
		ArrayList<EW_Alphabet> list = new ArrayList<EW_Alphabet>();
		try {
			ResultSet rs = getListByDetail(keys);
			while (rs.next()) {
				EW_Alphabet item = new EW_Alphabet(rs.getString("language"), rs.getString("letter"));
				item.wordsCount = rs.getInt("wordsCount");
				list.add(item);
			}
			rs.close();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * get one letter of alphabet
	 * If request return more then one letter function will return first letter.
	 * @param keys	array of request parameters key can be:
	 * 	1 - language get string, name of language
	 *  2 - letter get string, letter sign 
	 * @return	one letter in EW_Alphabet class.
	 */
	protected EW_Alphabet getOneItemByKey(HashMap<String, String> keys) {
		try {
			ResultSet rs = getListByDetail(keys);
			int getItem = 0;
			while (rs.next()) {
				this.languageName = rs.getString("language");
				this.letter = rs.getString("letter");
				this.wordsCount = rs.getInt("wordsCount");
				getItem++;
				rs.close();
				break;
			}
			if (getItem == 0)
				return null;
			else
				return this;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Delete all records which refer to the language
	 * @return
	 */
	public int deleteAlphabet(){
		ArrayList<String> keys = new ArrayList<String>();
		keys.add("language = '"+languageName+"'");
		
		return super.deleteOneRecord(keys);
	}
	
	/**
	 * insert current object in DB
	 * @return - executed result
	 */
	public int insertCurRecord(){
		HashMap<String, String> insertValues = new HashMap<String, String>();
		insertValues.put("LETTER".toUpperCase(), letter);
		insertValues.put("language".toUpperCase(), languageName);
		insertValues.put("wordsCount".toUpperCase(), ((Integer)wordsCount).toString());
		return super.insertOneRecord(insertValues);
	}
	
	/**
	 * update current object in DB
	 * @return - executed result
	 */
	public int updateCurrentRecord(){
		ArrayList<String> where = new ArrayList<String>();
		where.add("LETTER = '" + letter + "'");
		where.add("language = '" + languageName + "'");
		
		ArrayList<String> sets = new ArrayList<String>();
		sets.add("wordsCount = " + ((Integer)wordsCount).toString()); 
		
		return super.updateOneRecord(where, sets);
	}

}
